package ai.weed.qrcode.controller;

import ai.weed.qrcode.common.Response;
import ai.weed.qrcode.domain.DrugInfo;
import ai.weed.qrcode.domain.Pagination;
import ai.weed.qrcode.domain.queryparam.DrugInfoQueryParam;
import ai.weed.qrcode.service.DrugService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.taobao.api.ApiException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 药品管理controller
 *
 * @author zhong.wang
 * @date 2025/11/3
 **/
@RestController
@RequestMapping("/drugInfo")
public class DrugController {

    @Resource
    private DrugService drugService;

    /**
     * 根据药品追溯码获取药品信息
     * @param code
     * @return
     * @throws ApiException
     */
    @GetMapping("/getDrugInfoByCode")
    public Response<DrugInfo> getDrugInfoByCode(String code) throws ApiException{
        DrugInfo drugInfoByCode = drugService.getDrugInfoByCode(code);
        return Response.success(drugInfoByCode);
    }

    /**
     * 同步药品信息
     * @param code
     * @return
     * @throws ApiException
     */
    @GetMapping("/sync")
    public Response<Void> sync(String code) throws ApiException {
        drugService.syncDrugInfo(code);
        return Response.success(null);
    }

    /**
     * 获取药品列表
     * @return
     */
    @PostMapping("/list")
    public Response<List<DrugInfo>> list(@RequestBody DrugInfoQueryParam queryParam) {
        return Response.success(drugService.list(queryParam));
    }


    /**
     * 获取药品列表
     * @param queryParam
     * @return
     */
    @PostMapping("/pageList")
    public Response<Pagination<DrugInfo>> pageList(@RequestBody DrugInfoQueryParam queryParam) {
        Page<Object> page = PageMethod.startPage(queryParam.getPageNum(), queryParam.getPageSize());
        List<DrugInfo> list = drugService.list(queryParam);
        Pagination<DrugInfo> drugInfoPagination = new Pagination<>(list, queryParam.getPageNum(), queryParam.getPageSize(), page.getTotal());
        return Response.success(drugInfoPagination);
    }


    /**
     * 上传药品信息
     * @param drugInfo
     * @return
     */
    @PostMapping("/uploadDrugInfo")
    public Response<Boolean> uploadDrugInfo(@RequestBody DrugInfo drugInfo) {
        return Response.success(drugService.uploadDrugInfo(drugInfo));
    }

    /**
     * 删除药品信息
     * @param id
     * @return
     */
    @GetMapping("deleteById")
    public Response<Integer> deleteById(Long id) {
        return Response.success(drugService.deleteById(id));
    }
}
